package com.ruoyi.test.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.aiworkflow.services.ICozeWorkflow;
import com.ruoyi.aiworkflow.services.dto.MbtiQuestions;
import com.ruoyi.aiworkflow.services.dto.QuestionsDTO;
import com.ruoyi.test.domain.AiworkflowDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.test.domain.MhTestQuestion;
import com.ruoyi.test.service.IMhTestQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测试问题库Controller
 *
 * @author ruoyi
 * @date 2025-11-23
 */
@RestController
@RequestMapping("/MhQuestions/question")
public class MhTestQuestionController extends BaseController {

    @Autowired
    private IMhTestQuestionService mhTestQuestionService;


    @Autowired
    private ICozeWorkflow cozeWorkflow;


    /**
     * 查询测试问题库列表
     */
    @PreAuthorize("@ss.hasPermi('MhQuestions:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(MhTestQuestion mhTestQuestion) {
        startPage();
        List<MhTestQuestion> list = mhTestQuestionService.selectMhTestQuestionList(mhTestQuestion);
        return getDataTable(list);
    }

    /**
     * 导出测试问题库列表
     */
    @PreAuthorize("@ss.hasPermi('MhQuestions:question:export')")
    @Log(title = "测试问题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MhTestQuestion mhTestQuestion) {
        List<MhTestQuestion> list = mhTestQuestionService.selectMhTestQuestionList(mhTestQuestion);
        ExcelUtil<MhTestQuestion> util = new ExcelUtil<MhTestQuestion>(MhTestQuestion.class);
        util.exportExcel(response, list, "测试问题库数据");
    }

    /**
     * 获取测试问题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('MhQuestions:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mhTestQuestionService.selectMhTestQuestionById(id));
    }

    /**
     * 新增测试问题库
     */
    @PreAuthorize("@ss.hasPermi('MhQuestions:question:add')")
    @Log(title = "测试问题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MhTestQuestion mhTestQuestion) {
        return toAjax(mhTestQuestionService.insertMhTestQuestion(mhTestQuestion));
    }

    /**
     * 修改测试问题库
     */
    @PreAuthorize("@ss.hasPermi('MhQuestions:question:edit')")
    @Log(title = "测试问题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MhTestQuestion mhTestQuestion) {
        return toAjax(mhTestQuestionService.updateMhTestQuestion(mhTestQuestion));
    }

    /**
     * 删除测试问题库
     */
    @PreAuthorize("@ss.hasPermi('MhQuestions:question:remove')")
    @Log(title = "测试问题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mhTestQuestionService.deleteMhTestQuestionByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('MhQuestions:question:list')")
    //    @PreAuthorize("@ss.hasPermi('MhQuestions:question:')")
    @Log(title = "创建测试问题库--AI工作流", businessType = BusinessType.OTHER)
    @PostMapping("/getaicreate")
    public QuestionsDTO GetAiCreate(@RequestBody AiworkflowDTO dto) throws JsonProcessingException {
        QuestionsDTO questionsDTO = new QuestionsDTO();
        if (dto.getCount() >= 0 && dto.getCount() <= 50) {
            questionsDTO = cozeWorkflow.CreatTestQuestions(dto.getCount(),"");
        } else if (dto.getCount() >= 50 && dto.getCount() <= 100) {
            questionsDTO = cozeWorkflow.CreatTestQuestionsV2(dto.getCount());
        } else if (dto.getCount() > 100) {
            questionsDTO = cozeWorkflow.CreatTestQuestionsV2(dto.getCount());
        }
        return questionsDTO;
    }

    //    @PreAuthorize("@ss.hasPermi('MhQuestions:question:remove')")
    @Log(title = "同步创建测试问题库--AI工作流", businessType = BusinessType.OTHER)
    @PostMapping("/syncAiCreate")
    public AjaxResult syncAiCreate(@RequestBody AiworkflowDTO dto) throws JsonProcessingException {
//        if (dto.getCount() == 0) {
//            return null;
//        }
        if (dto.getQuestions().size() <= 0) {
            return null;
        }
        if (dto.getVersion().equals("")) {
            return null;
        }
        //
        int CreateCount = 0;
        for (MbtiQuestions question : dto.getQuestions()) {
            MhTestQuestion model = new MhTestQuestion();

//            BeanUtils.copyProperties(question, model);
            model.setText(question.getText());
            model.setOptionA(question.getOption_a());
            model.setOptionB(question.getOption_b());
            model.setDimension(question.getDimension());
            model.setScoreIfA(question.getScore_if_a());
            model.setScoreIfB(question.getScore_if_b());
            model.setSort(question.getId());
            model.setVersion(dto.getVersion());
            model.setStatus(1L);
            int i = mhTestQuestionService.insertMhTestQuestion(model);
            if (i > 0) {
                CreateCount++;
            }
        }
        return toAjax(CreateCount);
    }


}
